fix: Add lock for simultaneous printing#417
Merged
Merged
Conversation
fbee030 to
b2b0056
Compare
cc330a6 to
aad631d
Compare
aad631d to
5d225e6
Compare
Member
|
Thank you. LGTM! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When running multi-DUT tests, the output from different DUTs can be interleaved/garbled when two devices print at the same time. For example:
This happens because each DUT has its own
_listenfunction running in a separatemultiprocessing.Process(spawned via_ctx = multiprocessing.get_context('spawn')), and they all write to the same_stdout(sys.__stdout__) with no synchronization. Two processes can interleave their_stdout.write()+_stdout.flush()calls. This can causeexpectto fail.This pull request introduces a robust solution to prevent garbled or interleaved output when multiple DUT (Device Under Test) listener processes write to stdout concurrently. It does so by adding a session-scoped multiprocessing lock that is shared among all listener processes, ensuring serialized writes to stdout. The changes include the implementation of the lock, integration into the DUT factory and plugin, and comprehensive tests to verify data integrity in concurrent scenarios.
Stdout Locking and Listener Process Improvements:
_stdout_lock) to serialize stdout writes across all DUT listener processes, preventing garbled output when multiple DUTs print simultaneously. The lock is set up as a pytest fixture withautouse=True, ensuring it is always available and registered before any DUT is instantiated. (pytest-embedded/pytest_embedded/plugin.py,pytest-embedded/pytest_embedded/dut_factory.py) [1] [2] [3]_listenfunction and related process creation logic to accept and use the shared_stdout_lock, wrapping stdout writes with the lock when present. This ensures atomic output and prevents interleaving from concurrent processes. (pytest-embedded/pytest_embedded/dut_factory.py) [1] [2] [3]set_stdout_lockutility to update the module-level lock reference, facilitating global access for all listener processes. (pytest-embedded/pytest_embedded/dut_factory.py,pytest-embedded/pytest_embedded/plugin.py) [1] [2]Testing and Validation:
pytest-embedded/tests/test_base.py)Internal Refactoring:
pytest-embedded/pytest_embedded/plugin.py) [1] [2]These changes collectively improve the reliability and clarity of test output in multi-DUT environments, especially when tests are run in parallel.
Testing
Tested locally. It solves the issue and work reliably with both single and multi dut tests.